#include <nice2d/application.h>
#include <nice2d/background.h>
#include <nice2d/widgets/desktop.h>
#include <nice2d/widgets/label.h>
#include <nice2d/widgets/helplistener.h>

class HelpListenerData
{
public:
    std::shared_ptr<Label> label;
    sf::Keyboard::Key key;
};

HelpListener::HelpListener(const sf::String &text, sf::Keyboard::Key key):
    data(new HelpListenerData)
{
    data->label = std::make_shared<Label>();
    data->label->setText(text);
    data->label->setSize(250.0f,21.0f);

    data->key = key;

    auto style = std::make_shared<LabelStyle>();
    style->textColor = sf::Color::White;
    style->normalBackground = Background::create(sf::Color::Transparent);
    style->hoverBackground = style->normalBackground;
    style->characterSize = 15.0f;
    data->label->setControlStyle(style);

    auto desktop = GET_APPLICATION_COMPONENT(Desktop);
    auto dock = desktop->addWidget(data->label);
    dock->setDock(true);
    dock->setHorizontalMode(HMode_Right);
    dock->setVertical(VMode_Top);
    dock->setXOffset(3.0f);
    dock->setYOffset(3.0f);
}

HelpListener::~HelpListener()
{
}

bool HelpListener::onListener(std::shared_ptr<Message> message)
{
    if (message->getMessageType() == MessageType_SFML) {
        if (sf::Keyboard::isKeyPressed(data->key)) {
            data->label->setVisible(!data->label->isVisible());
            return true;
        }
    }
    return false;
}

